/******************************************************************************************************
Copyright (C) 2009 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

function showMessage(aMessage)
{
	MessageBox.information(aMessage, MessageBox.Ok);
}

function onRadioButtonToggled()
{
		if( this.checked )
			showMessage(this.objectName+" "+this.checked);
}

function onPrintLineEditText()
{
	showMessage(this.lineEdit.text);
}

function printValue()
{
	print("New value: "+this.value);
}

function onComboBoxChanged()
{
	print("ComboBox, selected: "+this.currentText);
}

function doOperation()
{
	var val1 = this.firstSpinBox.value;
	var val2 = this.secondSpinBox.value;
	print(val1);
	print(val2);
	print( this.operationComboBox.currentText );
	if( this.operationComboBox.currentText == "Sum" )
	{
		var total = val1+val2;
		showMessage("Sum: "+total);
		print(val1+val2);
	}
	else if( this.operationComboBox.currentText == "Product" )
	{
		var total = val1*val2;
		print(val1*val2);
		showMessage("Product: "+total);
	}
}

function dialogExample()
{
	var diag = new Dialog();
	diag.title="Test Dialog";

	var gBox = new GroupBox();
	gBox.objectName="groupBox";
	gBox.checkable=true;
	gBox.checked = true;
	gBox.title = "First Group Box";

	var lEditGroupBox = new GroupBox();
	var lEdit = new LineEdit();
	//Setting an object name to the line edit we will be able to recognize this object in the whole program
	lEdit.objectName="lineEdit";
	lEdit.label="Add Some Text:";
	lEdit.text="Some Text";
	var lEditPushButton = new PushButton();
	lEditPushButton.objectName="lEditPushButton";
	lEditPushButton.text="Print this text";
	//We connect the clicked event of the push button to the onPrintLineEditText.
	//The first argument of the function connect will we used as "this" object
	lEditPushButton.clicked.connect(lEditGroupBox,onPrintLineEditText);
	lEditGroupBox.add(lEdit,lEditPushButton);
	//when adding a widget to a dialog/group box the widget becomes a property with name widget.objectName of the dialog/goup box
	gBox.add(lEditGroupBox);

	var rButtonGroupBox = new GroupBox();
	rButtonGroupBox.objectName="rButtonsGroupBox";
	rButtonGroupBox.title="Radio Buttons Group";
	var rButton1 = new RadioButton();
	rButton1.objectName="radioButton1";
	rButton1.text="First";
	rButton1.checked=true;
	//The toggled signal of the Radio Button is emitted when the button changes its toggle state
	//We connect this signal with the onRadionButtonToggled
	rButton1.toggled.connect(rButton1,onRadioButtonToggled);
	rButtonGroupBox.add(rButton1);
	var rButton2 = new RadioButton();
	rButton2.objectName="radioButton2";
	rButton2.text = "Second";
	rButton2.toggled.connect(rButton2,onRadioButtonToggled);
	//when adding a widget to a dialog/group box the widget becomes a property with name widget.objectName of the dialog/goup box
	rButtonGroupBox.add(rButton2);
	//when adding a widget to a dialog/group box the widget becomes a property with name widget.objectName of the dialog/goup box
	gBox.add(rButtonGroupBox);

	var operationGroupBox = new GroupBox();
	var firstSpinBox = new SpinBox();
	firstSpinBox.objectName="firstSpinBox";
	firstSpinBox.minimum = 0;
	firstSpinBox.maximum = 10;
	firstSpinBox.value = 5;
	//the valueChanged signal is triggered every time the vale of the spin box changes
	firstSpinBox['valueChanged(int)'].connect(firstSpinBox,printValue);
	var secondSpinBox = new DoubleSpinBox();
	secondSpinBox.objectName="secondSpinBox";
	secondSpinBox.decimals=2;
	secondSpinBox.minimum = 0;
	secondSpinBox.maximum = 10;
	secondSpinBox.value = 5;
	secondSpinBox['valueChanged(double)'].connect(secondSpinBox,printValue);
	operationGroupBox.add("First:",firstSpinBox, "Second:",secondSpinBox);
	var pButton = new PushButton();
	pButton.objectName="pButton";
	pButton.text="Calculate";
	//we use the operationGroupBox as "this" object in the doOperation to access to all the widgets in the group box
	pButton.clicked.connect(operationGroupBox,doOperation);
	var cBox = new ComboBox();
	cBox.objectName="operationComboBox";
	cBox.addItem("Sum", "Product");
	print(cBox.items);
	cBox['activated(int)'].connect(cBox,onComboBoxChanged);
	//when adding a widget to a dialog/group box the widget becomes a property with name widget.objectName of the dialog/goup box
	operationGroupBox.add("Operation:", cBox, pButton);
	gBox.add(operationGroupBox);

	diag.add(gBox);
	
	lEdit.setFocus();
	
	diag.exec();
	return undefined;
}

function labelTest() {
	var ui = Application.loadUi('<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"><class>Dialog</class><widget class="QDialog" name="Dialog"><property name="geometry"><rect><x>0</x><y>0</y><width>400</width><height>300</height></rect></property><property name="windowTitle"><string>Dialog</string></property><layout class="QVBoxLayout" name="verticalLayout"><item><widget class="QLabel" name="label"><property name="text"><string>TextLabel</string></property></widget></item><item><widget class="QDialogButtonBox" name="buttonBox"><property name="orientation"><enum>Qt::Horizontal</enum></property><property name="standardButtons"><set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set></property></widget></item></layout></widget><resources/><connections><connection><sender>buttonBox</sender><signal>accepted()</signal><receiver>Dialog</receiver><slot>accept()</slot><hints><hint type="sourcelabel"><x>248</x><y>254</y></hint><hint type="destinationlabel"><x>157</x><y>274</y></hint></hints></connection><connection><sender>buttonBox</sender><signal>rejected()</signal><receiver>Dialog</receiver><slot>reject()</slot><hints><hint type="sourcelabel"><x>316</x><y>260</y></hint><hint type="destinationlabel"><x>286</x><y>274</y></hint></hints></connection></connections></ui>');
//	var pxm = new Pixmap(":/images/about.png");
	var pxm = new Pixmap();
	pxm.load(":/images/about.png");

	print(pxm.toString());
	ui.widgets.label.pixmap = pxm;
	ui.widgets.label.scaledContents = true;

	var halfPixmap = pxm.scaled(pxm.width / 2, pxm.height/2);
	print(halfPixmap.toString());

	ui.show()
}
